<?php
//error_reporting(0);
$config=require '../../../Common/Conf/upload_config.php';
require '../../../Common/Common/function.php';
if(!$_COOKIE['AUTH_USER_STRING'])die();
$fype=RemoveXSS($_GET['fype']); //上传类型
if(!$config[$fype])die();
$tourl=RemoveXSS($_GET['tourl']); //提交地址
$url=RemoveXSS($_GET['url']); //站点目录
$UpFile=RemoveXSS($_GET['UpFile']); //上传文件夹目录名称
?>
//-------------图片和文件显示------------------------
function adminFile(FileUrl,w,h,path) {
	var url = FileUrl;
	url = url.split(".");
	url = url[url.length-1].toLowerCase();
	var _data ="";
	if (url=="jpg" || url=="gif" || url=="png"){//图片
		_data+="<img src=\""+path+"/"+FileUrl+"\" rel=\""+path+"/"+FileUrl+"\" width=\""+w+"\" height=\""+h+"\"  border=0 onerror=this.src='Application/Management/Public/img/nopic.gif' style=\"float:left\">";
	}
	else if (url=="mp3" || url=="wma"){ //音频 invert()
		_data+="<EMBED style='FILTER: xray(); WIDTH: 100px; HEIGHT: "+ h +"px' src=\""+path+"/"+FileUrl+"\" type=application/x-mplayer2 volume='0' autostart='0' loop='-1'>";
	}
	else{
		_data=FileUrl;
	}
    
    if(FileUrl){
		return _data+'<a class="btn dark" id="del<?=$fype?>">删除</a>';
    }else{
		return _data;
    }
}
$(function(){
	$("#<?=$fype?>Name").css("width","70px");
	$("#<?=$fype?>submitUplod").click(function (){
		var parentH=$(this).parent("div");
        var file=parentH.find("input[type='file']");
        if(file.val()=="" || file.val()==null || file.val()== "undefined"){bootbox.alert("没有上传文件");return false;}
        parentH.find(".help-block preview").html("<img src='App/Manage/Public/images/loading.gif'>");
		var fileId=file.attr("id");
		var fType=$(this).attr("rel");
		var data = { fileType: fType }
		$.ajaxFileUpload ({
			//url:"<?=$tourl?>&fileType="+fType, //你处理上传文件的服务端
            url:"<?=$tourl?>", //你处理上传文件的服务端
			secureuri:false, //与页面处理代码中file相对应的ID值
			data: data,
			fileElementId:fileId,//文件选择框的id属性
			dataType: 'json', //返回数据类型:text，xml，json，html,scritp,jsonp五种
			success: function (data,status) {
				parentH.find(".help-block preview").html("");
				bootbox.alert(data.msg);
                if(data.error=='err'){return false;}
                var inputNew=$("#"+fType+"_path_new");
				if(inputNew.size()<1){
                	//alert('#'+fType+'_path');
                    if(fType=='newsmore')
                    $('#'+fType+'_path').attr('value',data.temurl);
                    //alert(data.temurl);
					parentH.append("<input type='hidden' name='"+fType+"_path_new' id='"+fType+"_path_new' value='"+data.imgurl+"'>");
				}else{
					parentH.find(inputNew).val(data.imgurl);
				}
                $("#<?=$fype?>Name").html(adminFile(data.temurl,100,100,'<?=$UpFile?>'));
                imagePreview();
                
				parentH.find("input[type='file']").val("");
			},
			error: function (data, status, e){
				bootbox.alert('上传失败');
				parentH.find(".help-block preview").html("");
			}
		})
	});
    var dataUrl=$("#<?=$fype?>_path").val();
    var dataHtml=adminFile(dataUrl,200,200,'<?=$UpFile?>');
    $("#<?=$fype?>Name").html(dataHtml);
    imagePreview();
    
    $("#del<?=$fype?>").click(function(){
    
    	if(!confirm('确定删除图片？')){
        	return false;
        }



    	$("#<?=$fype?>_path").val('');
        $("#<?=$fype?>_path_new").val('');
        $("#<?=$fype?>Name").html('');
    });
    
    
});

var _data="";
//_data+='<div class="divUpload">';
//_data+='	<span class="uploadF">';
//_data+='		<input type="file" id="file_<?//=$fype?>//" name="file" class="fileSub" onchange="<?//=$fype?>//Name.innerHTML=this.value" size="1" >';
//_data+='		<a>浏 览</a>';
//_data+='	</span>';
//_data+='	<a class="submitUplod" id="<?//=$fype?>//submitUplod" rel="<?//=$fype?>//">上 传</a>';
//_data+='	<span class="help-block preview" id="<?//=$fype?>//Name">&nbsp;</span>';
//_data+='</div>';


_data+='<input type="file" id="file_<?=$fype?>" name="file" class="default" style="position: absolute;left:16px;top: 0px;width:56px;height:34px;overflow:hidden; opacity: 0;filter:alpha(opacity=0);" onchange="<?=$fype?>Name.innerHTML=this.value" size="1" >';
_data+='<a class="btn blue" >浏览</a>&nbsp';
_data+='<a class="btn default" id="<?=$fype?>submitUplod" rel="<?=$fype?>">上传</a>';
_data+='<span class="help-block preview" id="<?=$fype?>Name">&nbsp;</span>';
document.write(_data);